home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_dos.lha / dos / sphsrc / sph_poly.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-26  |  2.4 KB  |  92 lines

  1. #include "HEADERS.h"
  2. #include <stdio.h>
  3. #include "sphigslocal.h"
  4. #include <string.h>
  5.  
  6. #ifdef THINK_C
  7. #include <stdlib.h>
  8. #endif
  9.  
  10. /** POLYHEDRON
  11. The routine in this file creates the actual polyhedron record.
  12. The structure database points to the memory allocated here.
  13. **/
  14.  
  15.  
  16.  
  17.  
  18. void
  19. SPH__freePolyhedron (POLYHEDRON *poly)
  20. {
  21.    facet *fptr = poly->facet_list;
  22.    int fcount = poly->facet_count;
  23.  
  24.    free (poly->vertex_list);
  25.  
  26.    while (fcount--) {
  27.       free (fptr->vertex_indices);
  28.       fptr = fptr++;
  29.    }
  30.  
  31.  
  32.    free (poly->facet_list);
  33.    free (poly);
  34. }
  35.  
  36.  
  37.  
  38.  
  39. /*!*/
  40. void
  41. SPH_polyhedron
  42.    (int numverts, int numfacets, point *verts, vertex_index *facets)
  43. {
  44.    register i;
  45.    register vertex_index *viptr;
  46.    vertex_index *start_viptr;
  47.    facet *fptr;
  48.    POLYHEDRON *newpoly;
  49.    vector vec1, vec2;
  50.  
  51.  
  52.    ALLOC_RECORDS (newpoly, POLYHEDRON, 1);
  53.    ALLOC_RECORDS (newpoly->vertex_list, MAT3hvec, numverts);
  54.    ALLOC_RECORDS (newpoly->facet_list, facet, numfacets);
  55.  
  56.    newpoly->vertex_count = numverts;
  57.    newpoly->facet_count = numfacets;
  58.  
  59.    /* COPY VERTICES, TRANSFORMING TO HVERTS. */
  60.    for (i=0; i<numverts; i++)
  61.       MAT3_SET_HVEC (newpoly->vertex_list[i] ,
  62.              verts[i][0], verts[i][1], verts[i][2], 1.0);
  63.  
  64.    /* SEPARATE LONG LIST OF VERTEX INDICES INTO INDIVIDUAL FACET DEF'S */
  65.    fptr = newpoly->facet_list;
  66.    viptr = facets;
  67.    while ((numfacets--) > 0) {
  68.       /* Calculate number of vertex indices in the next list */
  69.       start_viptr = viptr;
  70.       while (*(++viptr) != (-1));   /* scan forward to find next sentinel */
  71.       fptr->vertex_count = viptr - start_viptr;
  72.  
  73.       /* Allocate space for them, and copy into the new memory */
  74.       ALLOC_RECORDS (fptr->vertex_indices, vertex_index, fptr->vertex_count);
  75.       bcopy (start_viptr, fptr->vertex_indices, 
  76.              sizeof(vertex_index) * fptr->vertex_count);
  77.  
  78.       /* Calculate normal */
  79.       MAT3_SUB_VEC (vec1, newpoly->vertex_list[fptr->vertex_indices[1]],
  80.                    newpoly->vertex_list[fptr->vertex_indices[0]]);
  81.       MAT3_SUB_VEC (vec2, newpoly->vertex_list[fptr->vertex_indices[2]],
  82.                    newpoly->vertex_list[fptr->vertex_indices[1]]);
  83.       MAT3cross_product (fptr->normal, vec1, vec2);
  84.       fptr->normal[3] = 1.0;
  85.             
  86.       viptr++;   /* to move past sentinel to start of next list */
  87.       fptr++;    /* to move to next facet struct in the array of facets */
  88.    }
  89.    
  90.    SPH__add_polyhedron_element (newpoly);
  91. }
  92.